﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Web.Areas.Admin.Lib;

namespace Web.Areas.Admin.Controllers
{
    public class AccountController : BaseController
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            var e = filterContext.Exception;
            if (!filterContext.ExceptionHandled)
            {
                filterContext.Exception = null;
                filterContext.Result = Redirect("/Admin/Error/SystemError");
                filterContext.ExceptionHandled = true;
            }
            base.OnException(filterContext);
        }

        [HttpGet]
        public ActionResult Logon()
        {
            if (Account.ID > 0)
            {
                return Redirect("/admin/home/index");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Logon(string username, string password)
        {
            Admin.Lib.Account account = new Lib.Account();
            bool result = true;
            try
            {
                result = account.LogOn(username, password, true);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    status = false,
                    errmsg = ex.Message,
                }, JsonRequestBehavior.AllowGet);
            }
            if (!result)
            {
                return Json(new { status = false, errmsg = "用户名或密码错误", }, JsonRequestBehavior.AllowGet);
            }
            return Json(new
            {
                status = true,
                errmsg = "ok",
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult LogOut()
        {
            Account.LogOut();
            return Redirect("/admin/account/logon");
        }

        public ActionResult Index()
        {
            return View();
        }
    }
}
